<?php
namespace App\Domain;

use App\Common\Functions;
use Platform\Model\SystemStore as SystemStoreModel;
use Platform\Model\Admin as AdminModel;
use Platform\Model\SystemOrder as SystemOrderModel;
use Platform\Model\SystemGoods as SystemGoodsModel;
use Platform\Model\File as FileModel;
use Platform\Model\Order as OrderModel;
use PhalApi\Exception\BadRequestException;

class Store extends Functions{

    //获取专场列表
    public function storeList($where,$select,$order,$page,$perpage) {
        $model = new SystemStoreModel();
        $SystemOrderModel = new SystemOrderModel();
        $where['delete_flag'] = 'N';
        //处理筛选条件
        $count = $model->count($where,'id');
        $list = $model->getList($where,array(),$select,$order,$page,$perpage);
        if(!empty( $list )){
            //统计围观数
            foreach ( $list as $k => $v ){
                $order_count = $SystemOrderModel->count(array('delete_flag'=>'N','store_id'=>$v['store_id']));
                $list[$k]['order_num'] = $order_count;
            }
        }
        $data['items'] = $list;
        $data['total'] = $count;
        return $data;
    }

    //获取专场详情
    public function storeDetail($id,$select){
        $model = new SystemStoreModel();
        $SystemOrderModel = new SystemOrderModel();
        $SystemGoodsModel = new SystemGoodsModel();
        $AdminModel = new AdminModel();
        $storeData = $model->getData(array('id'=>$id),array(),$select);
        if( empty($storeData)&&$storeData['delete_flag']=='Y' ){
            throw new BadRequestException('专场不存在或已删除！');
        }
        //统计围观数
        $order_count = $SystemOrderModel->count(array('delete_flag'=>'N','store_id'=>$id));
        $storeData['order_num'] = $order_count;
        //统计拍品数
        $goods_count = $SystemGoodsModel->count(array('delete_flag'=>'N','store_id'=>$id));
        $storeData['goods_num'] = $goods_count;
        //查询主理人姓名
        $admin_info = $AdminModel->get($storeData['admin_id'],'name');
        $storeData['admin_name'] = $admin_info['name'];
        //增加专场围观数
        $model->update($id,array('look_num'=>new \NotORM_Literal("look_num + 1")));
        return $storeData;
    }

    //获取专场下的商品列表
    public function goodsList($where,$select,$order,$page,$perpage) {
        $model = new SystemGoodsModel();
        $where['delete_flag'] = 'N';
        //处理筛选条件
        $count = $model->count($where,'id');
        $list = $model->getList($where,array(),$select,$order,$page,$perpage);
        $data['items'] = $list;
        $data['total'] = $count;
        return $data;
    }

    //获取商品详情
    public function goodsDetail($id,$select){
        $model = new SystemGoodsModel();
        $FileModel = new FileModel();
        $SystemStoreModel = new SystemStoreModel();
        $AdminModel = new AdminModel();
        $OrderModel = new OrderModel();
        $goodsData = $model->getData(array('id'=>$id),array(),$select);
        if( empty($goodsData)&&$goodsData['delete_flag']=='Y' ){
            throw new BadRequestException('商品不存在或已删除！');
        }
        //查询商品轮播
        //处理商品banner
        $banner = $FileModel->getFileList(array('sgi.goods_id'=>$id,'sgi.type'=>1),'sf.att_upload_path as url','sgi.id ASC');
        $goods_banner_list = array();
        if( !empty( $banner ) ){
            foreach ( $banner as $k => $v ){
                $goods_banner_list[] = $v['url'];
            }
        }
        $goodsData['goods_banner'] = $goods_banner_list;
        //处理商品详情
        $detail = $FileModel->getFileList(array('sgi.goods_id'=>$id,'sgi.type'=>2),'sf.att_upload_path as url','sgi.id ASC');
        $goods_detail_list = array();
        if( !empty( $detail ) ){
            foreach ( $detail as $k => $v ){
                $goods_detail_list[] = $v['url'];
            }
        }
        $goodsData['goods_detail'] = $goods_detail_list;
        //查询专场状态
        $store_info = $SystemStoreModel->get($goodsData['store_id'],'status,store_member_name,store_avatar,admin_id,start_time,end_time');
        $goodsData['status'] = $store_info['status'];
        $goodsData['store_member_name'] = $store_info['store_member_name'];
        $goodsData['store_avatar'] = $store_info['store_avatar'];
        $goodsData['start_time'] = $store_info['start_time'];
        $goodsData['end_time'] = $store_info['end_time'];
        $goodsData['status'] = $store_info['status'];
        //查询主理人信息
        $admin_info = $AdminModel->get($store_info['admin_id'],'name,avatar');
        $goodsData['admin_name'] = $admin_info['name'];
        $goodsData['admin_avatar'] = $admin_info['avatar'];
        //查询所有该商品出价订单 todo
        $order_count = $OrderModel->getGoodsOrderCount(array('so.goods_id'=>$id,'so.delete_flag'=>'N'),'so.id');
        $order_list = $OrderModel->getGoodsOrder(array('so.goods_id'=>$id,'so.delete_flag'=>'N'),"sm.member_name,sm.member_avatar,sm.member_mobile,so.order_price,so.order_price,from_unixtime(so.add_time,'%Y-%m-%d %H:%i:%S') as add_time",'so.id DESC');
        $goodsData['orderList']['total'] = $order_count;
        $goodsData['orderList']['items'] = $order_list;
        //增加商品围观数
        $model->update($id,array('look_num'=>new \NotORM_Literal("look_num + 1")));
        return $goodsData;
    }
}
